'use strict';
/**
 * Created by zhouzhongyuan on 2016/1/12.
 */
$app.service(function (_) {
  return function (storevar, setter, getter) {
    if (
      _.isArray(storevar) &&
      storevar.length == 2 &&
      _.isObject(_.first(storevar)) &&
      _.isString(_.last(storevar))
    ) {
      var obj    = _.first(storevar);
      var objp   = _.last(storevar);
      var status = obj.hasOwnProperty(objp);
      if (_.isFunction(setter) && getter == undefined) {
        if (!status) {
          return obj[objp] = setter(function (data) {
            obj[objp] = data;
            return obj[objp];
          });
        }else {
          return obj[objp];
        }
      } else if (_.isFunction(setter) && _.isFunction(getter)) {
        !status ? setter(function (data) {
          obj[objp] = data;
          return obj[objp];
        }) : getter(function () {
          return obj[objp];
        })
      } else {
        setter(!(!status), function () {
          if (arguments.length > 0) {
            obj[objp] = _.first(arguments);
          }
          return obj[objp];
        }, function (data) {
          obj[objp] = data;
          return data;
        });
      }
    }
  };
});